package com.wzt.lianfirecontrol.fragment.my;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.fragment.BaseFragment;
import com.wzt.lianfirecontrol.http.HttpHelper;
import com.wzt.lianfirecontrol.http.json.ParseJsonData;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

import java.util.HashMap;

/**
 * 意见反馈
 */
public class AdviceBackFragment extends BaseFragment {

    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    private EditText et_advice;
    private TextView tv_text_num;
    private EditText et_weixin;
    private EditText et_phone;

    private Button bt_submit;

    private View include_no_data;
    private View include_no_net;
    private View include_progress_bar;

    private static final int ADVICE_BACK_REQUEST_WHAT = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView){
            bundle = getArguments();
            activity = getMyActivity();
            itemContentView = inflater.inflate(R.layout.f_advice_back, container, false);
            initView();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        et_advice = itemContentView.findViewById(R.id.et_advice);
        tv_text_num = itemContentView.findViewById(R.id.tv_text_num);
        et_advice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (StringUtils.isEmpty(et_advice.getText().toString())){
                    tv_text_num.setText("0/300");
                }else {
                    tv_text_num.setText(et_advice.getText().toString().length() + "/300");
                }

            }
        });
        et_weixin = itemContentView.findViewById(R.id.et_weixin);
        et_phone = itemContentView.findViewById(R.id.et_phone);

        bt_submit = itemContentView.findViewById(R.id.bt_submit);
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initHttpHelper();
            }
        });

        include_no_data = itemContentView.findViewById(R.id.include_no_data);
        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        InitView.instance().initProgressBar(activity, include_progress_bar);
        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });
    }

    /**
     * 获取列表
     */
    private void initHttpHelper() {
        if (Utils.hasNetwork(activity)){
            include_no_net.setVisibility(View.GONE);
        }else {
            include_no_net.setVisibility(View.VISIBLE);
            return;
        }
        HashMap<String, String> url_params = null;
        if (bundle != null && bundle.containsKey(ConstData.ACTIONPARAMS)){
            url_params = (HashMap<String, String>) bundle.getSerializable(ConstData.ACTIONPARAMS);
        }if (null == url_params){
            url_params = new HashMap<>();
        }
        if (null != et_advice && !StringUtils.isEmpty(et_advice.getText().toString())){
            url_params.put("advice", et_advice.getText().toString());
        }else {
            ToastUtils.showToast(activity, "建议内容不能为空");
            return;
        }
        if (null != et_weixin && !StringUtils.isEmpty(et_weixin.getText().toString())){
            url_params.put("adviceWeixin", et_weixin.getText().toString());
        }else {
            ToastUtils.showToast(activity, "微信号不能为空");
            return;
        }
        if (null != et_phone && !StringUtils.isEmpty(et_phone.getText().toString())){
            url_params.put("advicePhone", et_phone.getText().toString());
        }else {
            ToastUtils.showToast(activity, "手机号不能为空");
            return;
        }
//        String url = Url.getGetUrl(activity,  Url.HOME_URL, url_params);
//        HttpHelper homeListHttpHelper = new AdviceBackListHttpHelper(activity, url, ADVICE_BACK_REQUEST_WHAT, handler);
//        homeListHttpHelper.setParseJsonData(new AdviceBackParseJsonData());
//        homeListHttpHelper.getHttpRequest();
        include_progress_bar.setVisibility(View.VISIBLE);
        include_progress_bar.postDelayed(new Runnable() {
            @Override
            public void run() {
                include_progress_bar.setVisibility(View.GONE);
                ToastUtils.showToast(activity, "意见反馈成功");
                activity.finish();
            }
        }, 1500);
    }


    private class AdviceBackListHttpHelper extends HttpHelper{
        public AdviceBackListHttpHelper(BaseActivity activity, String url, int what, Handler handler) {
            super(activity, url, what, handler);
        }
    }

    protected class AdviceBackParseJsonData extends ParseJsonData {

        public AdviceBackParseJsonData() {
        }

        @Override
        public void analyzeResult(String dataStr, Bundle bundle, int what) {
            switch (what){
                case ADVICE_BACK_REQUEST_WHAT:
//                    try {
//                        if (objectData.has("list")){
//                            String rowsData = objectData.getString("list");
//                            LinkedList<BannerModel> bannerModels = (LinkedList<BannerModel>) JSONUtil.jsonArrayToBeanList(new JSONArray(rowsData), BannerModel.class);
//                            if (null != bannerModels && bannerModels.size() > 0){
//                                bundle.putSerializable("list", bannerModels);
//                            }
//                            bundle.putBoolean(REQUEST_OK, true);
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                        bundle.putBoolean(REQUEST_OK, false);
//                        bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
//                    }
                    break;
            }
        }
    }

    @Override
    protected void dealWithMyMsgs(Message msg) {
        super.dealWithMyMsgs(msg);
        Bundle bundle = msg.getData();
        if (!bundle.getBoolean(ParseJsonData.REQUEST_OK) ||
                !ParseJsonData.REQUEST_OK_CODE.equals(bundle.getString(ParseJsonData.ERROR_CODE))){
            //ToastUtils.showToast(activity, bundle.getString(ParseJsonData.ERROR_MSG));
            if (msg.what == ADVICE_BACK_REQUEST_WHAT) {
                include_progress_bar.setVisibility(View.GONE);
            }
            return;
        }
        switch (msg.what){
            case ADVICE_BACK_REQUEST_WHAT:
                include_progress_bar.setVisibility(View.GONE);
                break;
        }
    }
}
